package com.itheima.tlias.controller;

import com.itheima.tlias.annotation.LogOperation;
import com.itheima.tlias.exception.BusinessException;
import com.itheima.tlias.pojo.*;
import com.itheima.tlias.service.StudentService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * TODO
 *
 * @Author mr.luo
 * @Date 06 4月 2025 17:31
 */
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 分页查询所有员工
     */
    @GetMapping
    public Result pageQuery(StudentDTO studentDTO) {
        PageResult<StudentVo> pageStudentVO = studentService.pageQuery(studentDTO);
        return Result.success(pageStudentVO);
    }

    /**
     * 批量删除学员信息
     */
    @LogOperation
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        studentService.batchDelete(ids);
        return Result.success();
    }

    /**
     * 添加学员信息
     */
    @LogOperation
    @PostMapping
    public Result add(@RequestBody AddStudentDTO addStudentDTO) {
        studentService.add(addStudentDTO);
        return Result.success();
    }

    /**
     * 根据id查询学员信息
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        StudentVo studentVO = studentService.findById(id);
        return Result.success(studentVO);
    }

    /**
     * 修改学员信息
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody StudentVo studentVO) {
        studentService.update(studentVO);
        return Result.success();
    }

    /**
     * 违纪处理
     */
    @LogOperation
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable("id") Integer id, @PathVariable("score") Integer score) {
        studentService.violation(id, score);
        return Result.success();
    }

    /**
     * 批量导入学员信息
     * 上传的文件格式为：姓名，学号，性别，手机号，身份证号，是否为学院生，地址，毕业时间，班级id
     */
    @LogOperation
    @PostMapping("/import/{clazzId}")
    public Result importStudent(MultipartFile file, @PathVariable Integer clazzId) throws IOException {
        if (file.isEmpty()) {
            throw new BusinessException("上传文件为空");
        }
        studentService.importStudent(file, clazzId);
        return Result.success();
    }

    /**
     * 批量导出学员信息
     * excel
     */
    @LogOperation
    @GetMapping("/export")
    public Result exportStudent(HttpServletResponse response,
                                @RequestParam(required = false) Integer clazzId,
                                @RequestParam(required = false) String name,
                                @RequestParam(required = false) Integer degree
    ) throws IOException {
        studentService.exportExcel(response, clazzId, name, degree);
        return Result.success();
    }
}
